package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.LeaveInfoDto;
import com.zhangtai.modules.entity.employee.PromotionEntity;
import com.zhangtai.modules.entity.leave.LeaveInfoEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-09-07 19:36:18
 */
@Mapper
public interface LeaveInfoDao extends BaseMapper<LeaveInfoEntity> {


    @Select("<script>Select l.*,info.job_id,info.org_id,info.name,info.code,info.unit_id " +
            "from tb_leave_info l left join tb_employee_info info on info.oid = l.oid " +
            "<where> l.oid = #{oid} and l.is_delete = 0" +
            "<if test='startDate!=null and startDate !=&apos;&apos;'> and ( leave_begin_time &gt;= #{startDate} and leave_end_time &lt;= #{endDate})</if>" +
            "<if test='dealStatus!=null and dealStatus.size>0'> and l.deal_status in " +
            " <foreach item=\"deal\" index=\"index\" collection=\"dealStatus\" open=\"(\" separator=\",\" close=\")\">#{deal}</foreach>" +
            "</if>" +
            "</where> " +
            "order by create_time desc</script>")
    List<LeaveInfoDto> getLeaveList(@Param("oid")Long oid,
                                    @Param("startDate")String startDate,
                                    @Param("endDate")String endDate,
                                    @Param("dealStatus")List<Long> dealStatus,
                                          IPage<LeaveInfoDto> page);

    @Select("Select leave.*,info.job_id,info.org_id,info.name,info.code,info.unit_id " +
            "from tb_leave_info l left join tb_employee_info info on info.oid = l.oid " +
            "where l.id = #{id} and l.is_delete = 0 ")
    Map<String,Object> getLeaveInfo(@Param("id") Long id);


    @Select("<script>Select l.*,info.job_id,info.org_id,info.name,info.code,info.unit_id " +
            "from tb_leave_info l left join tb_employee_info info on info.oid = l.oid " +
            "<where> l.is_delete = 0 and l.deal_status !=0 " +
            "<if test='orgIds!=null and orgIds.size>0'> and l.unit_id in " +
            " <foreach item=\"o\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{o}</foreach>" +
            "</if>" +
            "<if test='unitIds!=null and unitIds.size>0'> and info.unit_id in " +
            " <foreach item=\"unitId\" index=\"index\" collection=\"unitIds\" open=\"(\" separator=\",\" close=\")\">#{unitId}</foreach>" +
            "</if>" +
            "<if test='orgId!=null and orgId.size>0'> and info.org_id in " +
            " <foreach item=\"org\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{org}</foreach>" +
            "</if>" +
            "<if test='jobIds!=null and jobIds.size>0'> and l.job_id in " +
            " <foreach item=\"jobId\" index=\"index\" collection=\"jobIds\" open=\"(\" separator=\",\" close=\")\">#{jobId}</foreach>" +
            "</if>" +
            "<if test='jobIds!=null and jobIds.size==0'> and l.job_id =null </if>" +
            "<if test='unitIds!=null and unitIds.size==0'> and info.org_id =null </if>" +
            "<if test='orgIds==null or orgIds.size ==0'> and l.unit_id = null  </if>" +
            "<if test='type!=null '> and l.type = #{type}  </if>" +
            "<if test='name!=null and name!=&apos;&apos; '> and info.name like '%${name}%'  </if>" +
            "</where> order by create_time desc</script>")
    List<LeaveInfoDto> getLeaveListSystem(@Param("orgIds")List<Long> orgIds,
                                          @Param("unitIds")List<Long> unitIds,
                                          @Param("jobIds")List<Long> jobIds,
                                          @Param("orgId")List<Long> orgId,
                                          @Param("type")String type,
                                          @Param("name")String name,
                                          Page<LeaveInfoDto> page);
}
